package com.xusheng.cli.controller.user;

import cn.hutool.core.date.DatePattern;
import cn.hutool.core.util.DesensitizedUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xusheng.cli.common.base.MyPage;
import com.xusheng.cli.common.base.Result;
import com.xusheng.cli.common.validation.Save;
import com.xusheng.cli.common.validation.Update;
import com.xusheng.cli.entity.commentModule.Comment;
import com.xusheng.cli.entity.frontUserModule.FrontUser;
import com.xusheng.cli.entity.videoModule.CommentVO;
import com.xusheng.cli.service.CommentService;
import com.xusheng.cli.service.FrontUserService;
import com.xusheng.cli.tools.CurrentPlayerUserUtil;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.constraints.Size;
import java.io.Serializable;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;

/**
 * (Comment)表控制层
 *
 * @author whr
 * @since 2023-11-20 11:04:19
 */
@Validated
@RestController
@RequestMapping("/user/v1/comment")
public class CommentController {
    /**
     * 服务对象
     */
    @Resource
    private CommentService commentService;
    @Resource
    private FrontUserService frontUserService;

    /**
     * 查询所有数据
     *
     * @param comment 查询实体
     * @return 所有数据
     * @author whr
     * @since 2023-11-20 11:04:19
     */
    @GetMapping("queryAll")
    public Result<List<Comment>> queryAll(Comment comment) {
        List<Comment> list = commentService.list(new LambdaQueryWrapper<>(comment)
                .gt(comment.getStartTime() != null, Comment::getCreateTime, comment.getStartTime())
                .lt(comment.getEndTime() != null, Comment::getCreateTime, comment.getEndTime()));
        for (Comment comment1 : list) {
            FrontUser user = frontUserService.getById(comment1.getUserId());
            comment1.setAccount(DesensitizedUtil.mobilePhone(user.getAccount()));
        }
        return Result.success(list);
    }

    @GetMapping("queryAll/short")
    public Result<List<CommentVO>> queryAllShort(Comment comment) {
        List<CommentVO> commentVOList = new ArrayList<>();
        List<Comment> list = commentService.list(new LambdaQueryWrapper<>(comment)
                .gt(comment.getStartTime() != null, Comment::getCreateTime, comment.getStartTime())
                .lt(comment.getEndTime() != null, Comment::getCreateTime, comment.getEndTime()));
        for (Comment comment1 : list) {
            CommentVO commentVO = new CommentVO();
            FrontUser user = frontUserService.getById(comment1.getUserId());
            commentVO.setUserID(comment1.getUserId().toString());
            commentVO.setUsername(DesensitizedUtil.mobilePhone(user.getAccount()));
            commentVO.setPinlunContent(comment1.getComment());
            commentVO.setTime(comment1.getCreateTime().format(DateTimeFormatter.ofPattern(DatePattern.NORM_DATETIME_PATTERN)));
            commentVOList.add(commentVO);
        }
        return Result.success(commentVOList);
    }

    @GetMapping("count")
    public Result<Integer> getCount(Comment comment) {
        Integer count = (int) commentService.count(new LambdaQueryWrapper<>(comment)
                .gt(comment.getStartTime() != null, Comment::getCreateTime, comment.getStartTime())
                .lt(comment.getEndTime() != null, Comment::getCreateTime, comment.getEndTime()));
        return Result.success(count);
    }

    /**
     * 分页查询所有数据
     *
     * @param page    分页对象
     * @param comment 查询实体
     * @return 所有数据
     * @author whr
     * @since 2023-11-20 11:04:19
     */
    @GetMapping("queryByPage")
    public Result<MyPage<Comment>> queryByPage(MyPage<Comment> page, Comment comment) {
        MyPage<Comment> page1 = commentService.page(page, new QueryWrapper<>(comment));
        List<Comment> records = page1.getRecords();
        for (Comment comment1 : records) {
            FrontUser user = frontUserService.getById(comment1.getUserId());
            comment1.setAccount(DesensitizedUtil.mobilePhone(user.getAccount()));
        }
        return Result.success(page1);
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     * @author whr
     * @since 2023-11-20 11:04:19
     */
    @GetMapping("queryOne/{id}")
    public Result<Comment> selectOne(@PathVariable Serializable id) {
        return Result.success(commentService.getById(id));
    }

    /**
     * 新增数据
     *
     * @param comment 实体对象
     * @return 新增结果
     * @author whr
     * @since 2023-11-20 11:04:19
     */
    @PostMapping("insert")
    public Result<Object> insert(@Validated(Save.class) @RequestBody Comment comment) {
        comment.setUserId(CurrentPlayerUserUtil.getUserId());
        return Result.isSuccess(commentService.save(comment));
    }

    /**
     * 修改数据
     *
     * @param comment 实体对象
     * @return 修改结果
     * @author whr
     * @since 2023-11-20 11:04:19
     */
    @PutMapping("update")
    public Result<Object> update(@Validated(Update.class) @RequestBody Comment comment) {
        comment.setUserId(CurrentPlayerUserUtil.getUserId());
        return Result.isSuccess(commentService.updateById(comment));
    }

    /**
     * 删除单条数据
     *
     * @param id 主键
     * @return 删除结果
     * @author whr
     * @since 2023-11-20 11:04:19
     */
    @DeleteMapping("delete/{id}")
    public Result<Object> delete(@PathVariable Long id) {
        Comment comment = new Comment();
        comment.setId(id);
        return Result.isSuccess(commentService.removeById(comment));
    }

    /**
     * 批量删除数据
     *
     * @param idList 主键结合
     * @return 删除结果
     * @author whr
     * @since 2023-11-20 11:04:19
     */
    @DeleteMapping("batchDelete")
    public Result<Object> batchDelete(@Validated @Size(min = 1, message = "缺少删除所需的id") @RequestParam("idList") List<Long> idList) {
        return Result.isSuccess(commentService.batchDelete(idList, new Comment()));
    }
}

